//
//  ManleyCityLibrary.m
//  ManleyCityLibrary
//
//  Created by ios_001 on 2018/7/17.
//  Copyright © 2018年 ios_001. All rights reserved.
//

#import "ManleyCityLibrary.h"

#import "AppCommonThirdConfig.h"
//#import "FGLoginViewController.h"
#import "LoginManager.h"
//#import "FGLoginModel.h"
//#import "FGLoginViewModel.h"
#import "FGLaunchViewModel.h"
#import "FGLaunchModel.h"
#import <CoreTelephony/CTCellularData.h>

#import "interfaceAPI.h"
#import "FGNetworkDomainManager.h"
//#import "AppDelegate.h"

//#define NETWORK_NOT_RESTRICTED @"network_not_restricted"

@implementation ManleyCityLibrary

+ (ManleyCityLibrary *)sharedManager {
    static ManleyCityLibrary *_sharedManager = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedManager = [[self alloc] init];
    });
    
    return _sharedManager;
}


+(void)startAPPLaunchConfig
{
    [UIApplication sharedApplication].idleTimerDisabled = YES;
    
    //1.获取网络权限 根绝权限进行人机交互
    if (__IPHONE_10_0) {
        [self networkStatus:nil didFinishLaunchingWithOptions:nil];
    }
    //    else {
    //        //2.2已经开启网络权限 监听网络状态
    //        [self addReachabilityManager:application didFinishLaunchingWithOptions:launchOptions];
    //    }
    
    [AppCommonThirdConfig commThirdConfig:nil];

    if (INTERNET_UAT == 1) {
        NSArray *hostArrar = @[@"https://api-user.zczx2255.com/",
                               @"https://api-user.zfol555.com/",
                               @"https://api-user.zfol666.com/",
                               @"https://api-user.zfol888.com/",
                               @"https://api-user.zfol999.com/"];
        
        FG_HTTP_Main_Domain = [[FGNetworkDomainManager shareInstance]startNetworkReachUrl:@"https://api-user.zczx2255.com/api/fg/appinfo?type=domain" initHostArr:hostArrar];
        
        if (FG_HTTP_Main_Domain.length == 0 || !FG_HTTP_Main_Domain) {
            FG_HTTP_Main_Domain = @"https://www.baidu.com/";
        }
    }
    else {
        NSArray *hostArrar = @[@"https://api-user.zfol-preproduce.com/",
                               @"https://api-user.zfol555.com/",
                               @"https://api-user.zfol666.com/",
                               @"https://api-user.zfol888.com/",
                               @"https://api-user.zfol999.com/"];
        
        FG_HTTP_Main_Domain = [[FGNetworkDomainManager shareInstance]startNetworkReachUrl:@"https://api-user.zfol-preproduce.com/api/fg/appinfo?type=domain" initHostArr:hostArrar];
        
        if (FG_HTTP_Main_Domain.length == 0 || !FG_HTTP_Main_Domain) {
            FG_HTTP_Main_Domain = @"https://www.baidu.com/";
        }
    }
}

+(void)applicationDidBecomeActive
{
    [[NSNotificationCenter defaultCenter] postNotificationName:MY_APPLICATION_DID_BECOME_ACTIVE object:@""];
    
    [[LoginManager instance] startALightWeightHttpServer];
}

+(void)applicationDidEnterBackground
{
    [UIApplication sharedApplication].idleTimerDisabled = NO;
}

+(void)applicationWillEnterForeground
{
    [UIApplication sharedApplication].idleTimerDisabled = YES;
}

+(void)applicationWillTerminate
{
    [UIApplication sharedApplication].idleTimerDisabled = NO;
}

/*
 CTCellularData在iOS9之前是私有类，权限设置是iOS10开始的，所以App Store审核没有问题
 获取网络权限状态
 */
+ (void)networkStatus:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //2.根据权限执行相应的交互
    CTCellularData *cellularData = [[CTCellularData alloc] init];
    
    /*
     此函数会在网络权限改变时再次调用
     */
    cellularData.cellularDataRestrictionDidUpdateNotifier = ^(CTCellularDataRestrictedState state) {
        switch (state) {
            case kCTCellularDataRestricted:
                
                NSLog(@"Restricted");
                //2.1权限关闭的情况下 再次请求网络数据会弹出设置网络提示
                
                break;
            case kCTCellularDataNotRestricted:
                
                NSLog(@"NotRestricted");
                //2.2已经开启网络权限 监听网络状态

                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    [[NSNotificationCenter defaultCenter] postNotificationName:NETWORK_NOT_RESTRICTED object:nil];
                });
                
                break;
            case kCTCellularDataRestrictedStateUnknown:
                
                NSLog(@"Unknown");
                //2.3未知情况 （还没有遇到推测是有网络但是连接不正常的情况下）
                
                break;
                
            default:
                break;
        }
    };
}

@end
